/**
 * 表单填报 编辑页
 *
 * 作者 zhangwei
 * 日期 2024-09-05 08:53:07
 */
var demojs = [],formVal={}
//JavaScript代码区域
layui.use(['formDesigner', 'form','layer','upload','element','febs'], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var upload  = layui.upload;
    var element = layui.element
    var index = layui.index,
        febs = layui.febs
    var formDesigner = layui.formDesigner;
    var form = layui.form;
    var render
    var $view = $('#formInput')

    if (formFillData !== undefined) {
        demojs = JSON.parse(formFillData.formHtml)
        if(formFillData.formJson){
            formVal =  JSON.parse(formFillData.formJson)
        }
        render = formDesigner.render({
            elem: '#formInput',
            data: demojs,
            viewOrDesign:true,
            previewElem:'#formInput',
            formData: formVal
        });
        //全局禁用表单
        render.globalDisable()
        var images = render.getImages();

        // 编辑回显图片和文件
        initImg()
        function initImg(){
            if(images && images.length>0){
                images.forEach(item => {
                    let imgHtml = ''
                    if(formVal[item.id] && formVal[item.id].length>0){
                        formVal[item.id].forEach( imgItem => {
                            imgHtml+='<div id="" class="file-iteme" style="position: relative;border: 1px solid #eee;">' +
                                '<img style="width: 100px;height: 100px;" data-filepath="'+ imgItem.filePath +'" data-filename="'+ imgItem.fileName +'" data-smallpath="'+ imgItem.smallPath +'" src=' + ctx + imgItem.filePath + '>' +
                                '<div class="info">' + imgItem.fileName + '</div>' +
                                '</div>'
                        })
                    }
                    $('#uploader-list-'+item.id).append(imgHtml)
                    $('#uploader-list-'+item.id).parents('.layui-input-block').show()
                })
            }
        }
        // 隐藏上传图片和上传文件按钮
        $view.find('.uploadImgBtn').hide()
        var filesData = render.getFiles();
        for (var i = 0; i < filesData.length; i++) {
            let elem = $view.find('#' + filesData[i].select)
            $(elem).hide()
        }
        initFiles()
        function initFiles(){
            if(filesData && filesData.length>0){
                filesData.forEach(item => {
                    let fileHtml = ''
                    if(formVal[item.id] && formVal[item.id].length>0){
                        $('#list-'+item.id).empty()
                        formVal[item.id].forEach( fileItem => {
                            fileHtml+='<tr id="upload-' + item.id + '" data-filesize="'+ fileItem.fileSize +'" data-filepath="'+ fileItem.filePath +'" data-filename="'+ fileItem.fileName +'" data-smallpath="'+ fileItem.smallPath +'">'+
                                '<td>' + fileItem.fileName + '</td>'+
                                '<td>' + fileItem.fileSize + '</td>'+
                                '<td><div class="layui-progress" lay-filter="progress-demo-' + item.id + '"><div class="layui-progress-bar" lay-percent="100%"></div></div></td>'+
                                '<td>'+
                                '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'+
                                '</td>'+
                                '</tr>'
                            $('#list-'+item.id).append(fileHtml)
                            $('#list-'+item.id).parents('.layui-input-block').show()
                            element.progress('progress-demo-' + item.id, '100%'); //执行进度条。n 即为返回的进度百分比
                        })
                    }

                })

            }
        }
    }
});